<template>
<div class="definitions">
    <p v-if="!props.searchWord" class="tip">Star by typing a word in search...</p>
    <p v-else-if="props.searchWord && !refurl.length">Sorry the definition of this word was found.</p>
    <div v-else class="definition" v-for="(definition,index) in refurl" :key="index">
        <!-- {{definition}} -->
        <!-- 音标读音 -->
        <div class="phpnetics">
            <span class="phonetic" @click="handleClick(index)">{{definition?.phonetics[0]?.text}}</span>
            <i class="iconfont icon-yangshengqi" @click="handleClick(index)"></i>
            <audio :src="definition?.phonetics[0]?.audio" ref="audio"></audio>
        </div>
        <div class="meanings">
            <div class="meaning" v-for="(meaning,index) in definition.meanings" :key="index">
                <!-- {{meaning}} -->
                <span class="partOfSpeech">{{meaning.partOfSpeech}}</span>
                <span class="word-meaning">
                    {{
                        meaning.definitions.map(definition=>definition.definition).join(" ")
                    }}
                </span>
            </div>
        </div>
    </div>
</div>

</template>

<script setup>
import axios from "axios";
import { watch,ref} from "vue";
const props = defineProps({
    searchWord:String,
});
//通过得到的这个searchWord来监听搜索的关键词有没有变化，有变化的话就转换一下请求

// watch( () => props.searchWord,(newValue)=>{
//     console.log(newValue);
// })
//发送请求
watch ( ()=>props.searchWord ,getdata)
//得到的结果会是个数组
const refurl = ref([]);

async function getdata(newValue){
    const url = `https://api.dictionaryapi.dev/api/v2/entries/en/${newValue}`;
    //每次请求前先清空一下，防止前一次的请求报错存留下来，以免catch的value值还是之前的
    refurl.value = [];
    try{
        const data = await axios.get(url);
        refurl.value = data.data;
    }catch(err){
        console.log(err);
    }
}

const audio = ref();
function handleClick(index){
    audio.value[index].currentTime = 0;
    audio.value[index].play();
}
</script>

<style lang="scss" scoped>
::-webkit-scrollbar{
    width: 5px;
    background: black;
}
::-webkit-scrollbar-thumb{
    background: rgba(255, 255, 255, 0.6);
    border-radius: 20px;
}
.definitions{
    border:10px solid #696969;
    border-radius: 10px;
    height: 65vh;
    padding: 15px;
    overflow-y: scroll;
    display: flex;
    flex-direction: column;
    gap: 5px;
    margin-top: 20px;
}
.tip{font-size: 18px;}
.definition{
    margin-bottom: 10px;
    .phpnetics{
        margin-bottom: 10px;
        .phpnetic{
            cursor: pointer;
            border-radius: 5px;
            margin-right: 10px;
            &:hover{opacity: 0.8;}
        }
        .iconfont{
            cursor: pointer;
            margin-left: 10px;
            &:hover{opacity: 0.8;}
        }
    }
    &:last-child{
        .meanings{border-bottom: none;}
    }
}
.meanings{
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding-bottom: 10px;
    border-bottom: 1px solid #c5bcbc;
    div.meaning{
        .partOfSpeech{
            margin-right: 10px;
            background-color:#3b3e45;
            opacity: 0.8;
            display: inline-block;
            padding-block: 3px;
            padding-inline: 5px;
            font-size: 14px;
            border-radius:5px;
            line-height: 25px;
        }
        .word-meaning{
            line-break: 25px;
        }
    }
}


</style>
